home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / arp68bk.zoo / arp.src / prg_6jc.s < prev    next >
Text File  |  1985-11-20  |  13KB  |  279 lines

  1.  ; PROGRAM NAME: PRG_6JC.S
  2.  ;      VERSION: 1.001
  3.  
  4.  ; Assembly Instructions:
  5.  
  6.  ;    Assemble in Relocatable mode and save with a TOS suffix.
  7.  
  8.  ; Execution Instructions:
  9.  
  10.  ;    Execute from the desktop.  Some versions of the ST operating system
  11.  ; may require the printer to be turned on for proper operation of this
  12.  ; trap handler.
  13.  
  14.  ; Program Function:
  15.  
  16.  ;    This program establishes itself in memory as a trap #13 handler.  While
  17.  ; the program is resident, it intercepts all trap #13 calls.  If an
  18.  ; intercepted call's function is $3, and if the device involved is the video
  19.  ; screen, then the custom trap handler redirects the call to include output
  20.  ; to the printer as well as to the video screen.  In this manner all text
  21.  ; output to the screen, which is accomplished by BIOS function $3 calls
  22.  ; (GEMDOS calls $2 and $9 are included because these functions rely on
  23.  ; BIOS function $3.) is sent to both the printer and the video screen.
  24.  
  25.  ;    All redirected ASCII codes below $1B, except those for a carriage
  26.  ; return and a linefeed, are filtered out of the data sent to the printer.
  27.  ; This prevents certain ASCII codes that are suitable for the screen but
  28.  ; which are undesirable for the printer from reaching the printer.
  29.  
  30.  ; MAJOR NOTE:
  31.  
  32.  ;    If this program is to be used when a software print buffer is to be
  33.  ; simultaneously resident, then the software print buffer program MUST be
  34.  ; executed first.  That is, the software print buffer must already be
  35.  ; resident when this program is executed.
  36.  
  37.  ; Program Features:
  38.  
  39.  ; 1. Produces a hardcopy of program input and output that is sent to the  
  40.  ;    screen.  This program eliminates the necessity of providing statements
  41.  ;    in the program to accomplish that task.  It permits the printer listing
  42.  ;    of a program to be followed by a printer listing of user/program
  43.  ;    interaction.  Especially useful for providing the results of an 
  44.  ;    execution for homework problems, or for programs under development.
  45.  
  46.  ; 2. When compiling, compiler output to the screen will be sent to the
  47.  ;    printer.   Especially useful when debugging the error messages
  48.  ;    that appear on the screen.  The printer output lets you go back to
  49.  ;    the editor with the error list hardcopy.  
  50.  
  51.  ; 3. If the Show button is selected from the Show/Print/Cancel Desktop
  52.  ;    dialog box, when this program is resident, the data which appears
  53.  ;    on the video screen will also be sent to the printer.
  54.  
  55. program_start:                     ; Calculate program size and retain result.
  56.  lea        program_end(pc), a3    ; Fetch program end address.
  57.  movea.l    4(a7), a4              ; Fetch basepage address.
  58.  suba.l     a4, a3                 ; Program size is in A3.
  59.  lea        stack(pc), a7          ; Provide a user stack.
  60.  
  61. install_new_trap_13_vector:
  62.  pea       custom_trap_handler(pc) ; Push new trap handler address onto stack.
  63.  move.w    #$2D, -(sp)             ; Push trap 13 vector number.
  64.  move.w    #5, -(sp)               ; Function = setexec = BIOS $5.
  65.  trap      #13                     ; Current trap handler vector returned in D0.
  66.  addq.l    #8, sp
  67.  move.l    d0, preempted_handler_address
  68.  
  69. relinquish_processor_control:      ; Maintain memory residency.
  70.  move.w    #0, -(sp)               ; See page 121 of Internals book.
  71.  move.l    a3, -(sp)               ; Program size.
  72.  move.w    #$31, -(sp)             ; Function = ptermres = GEMDOS $31.
  73.  trap      #1
  74.  
  75.  ; NOTE:
  76.  
  77.  ;      The custom trap #13 handler is entered each time an application invokes
  78.  ; the trap #13 call.  If the call does not involve printing a character to the
  79.  ; screen, then a jump is performed to the preempted trap #13 handler.
  80.  
  81.  ;      If the call involves printing an escape sequence (An escape sequence
  82.  ; is a two-character code, the first of which is $1B or 27 decimal.  Escape
  83.  ; sequences provide screen control--see section 3.6 "The Atari VT52 Emulator",
  84.  ; pages 245-249 of the Internals book.) for screen control, then the sequence
  85.  ; is sent to the screen via the preempted trap #13 handler.
  86.  
  87.  ;      Otherwise, for each trap #13 call that is also a bconout invocation
  88.  ; with device code #2, the custom_trap_13 routine is entered three times.
  89.  ; The first time that the handler is entered, a variable is initialized, then,
  90.  ; when it is entered subsequently, a jump, over the initialization sequence,
  91.  ; to the preempted trap handler is performed.  The custom trap handler is
  92.  ; entered three times because the custom handler prints to both the screen
  93.  ; and the printer by invoking its own trap #13 calls; those calls are also
  94.  ; intercepted by the custom handler.
  95.  
  96.  ;      The custom handler must be able to handle trap #13 calls made while
  97.  ; the processor is in supervisor mode or user mode.
  98.  
  99. custom_trap_handler:
  100.  tst.b     initialization_flag
  101.  bne       skip_initialization
  102.  
  103.  ; Processing the stack data:
  104.  
  105.  ;      The location of the stack data that must be processed by this subroutine
  106.  ; depends on the state of the processor when the exception occurs.  If it was
  107.  ; in the supervisor state, then the data will be stacked, as indicated, at the
  108.  ; following relative locations:
  109.  
  110.  ;                    location - 11 = character to be printed, byte length
  111.  ; Old Top of Stack:  location - 10 = character to be printed, word length
  112.  ;                    location -  8 = device to which character is to sent
  113.  ;                    location -  6 = bios command to be executed
  114.  ;                    location -  4 = program counter low word
  115.  ;                    location -  2 = program counter high word
  116.  ; SSP ->             location -  0 = invoking program's status register content
  117.  
  118.  ; In each case, above, the location listed is the location of the most
  119.  ; significant byte of the data listed.  For example, relative location 10
  120.  ; contains the most significant byte of the word which specifies the
  121.  ; character to be printed.  Relative location 11 contains the least
  122.  ; significant byte of that word.  Actually, relative location 11 is
  123.  ; "the least significant byte" in computer vernacular, but, in fact, it
  124.  ; is that byte which contains the character code.  The byte at relative
  125.  ; location 10 contains only zeroes.
  126.  
  127.  ; The supervisor stack pointer (SSP) will be pointing to the new top of
  128.  ; stack; the data there will be the content of the most significant byte
  129.  ; of the status register, as it was when the exception occurred.  Now, as
  130.  ; it turns out, this byte will be the one of significance, as far as the
  131.  ; trap handler is concerned.
  132.  
  133.  ; If the processor was in the user state, then the data will be stacked,
  134.  ; as indicated, at the following relative locations:
  135.  
  136.  ; Old Top of Stack:  location -  4 = character to be printed
  137.  ;                    location -  2 = device to which character is to sent
  138.  ; USP ->             location -  0 = bios command to be executed
  139.  
  140.  ; The user stack pointer (USP) will be pointing to the top of the stack;
  141.  ; the data there will be the bios command to be executed.
  142.  
  143.  ; In order to process the stack data without regard to the processor state
  144.  ; before invocation, if the processor was in user mode, then the offsets used
  145.  ; to access the stack data with the USP as reference must be adjusted so that
  146.  ; they match the offsets used to access the stack data with the SSP as
  147.  ; reference.  Therefore, if the processor was in user mode when the trap #13
  148.  ; call was made, the value six is subtracted from the register that is used
  149.  ; to access the data.
  150.  
  151.  ; Then, common offset values, which will access the data correctly regardless
  152.  ; of the stack pointer used as reference can be used.
  153.  
  154.  ; When we begin, we know that the processor is now is supervisor mode,
  155.  ; however, we must determine its state at the time of the exception.  That
  156.  ; processor state must be checked by testing bit 13 of the status register.
  157.  ; We know that the SSP is now pointing to the content of the status register
  158.  ; as it was at exception time.  In fact, it is pointing to the most significant
  159.  ; byte of the status register word.
  160.  
  161.  ; Ther